package com.neusoft.common.util;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.common.Const;

/**
 * 项目的工具类
 * Created By YZ at Jun 30, 2010 10:53:47 AM
 * Class:util
 *
 */
public class util{
    /**
     * 获取当前系统时间字符串
     *
     * @return
     */
    public static String getSysdateStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }



    /**
     * 判断字符串是否既不为null又不为空字符串；是为false，否则为true
     *
     * @param str
     * @return
     */
    public static boolean isNull(String str){
        if(str != null && !str.trim().equals("")){
            return false;
        }
        return true;
    }

    /**
     * select生成器，根据对应所要生成的value－name关系map
     *
     * @param map
     * @param selectName       select的name
     * @param selectID         select的id
     * @param selectedValue    被选中值
     * @param isFull           是否包含不限选项
     * @param fullOptionsValue 不限选项值
     * @param jsString         js代码
     * @param isFullName       不限选项的现实名称，不填写默认为“不限”
     *
     * @return
     */
    public static String selectGenerator(Map<String, String> map,
            String selectName, String selectID, String selectedValue,
            boolean isFull, String fullOptionsValue, String jsString,String...isFullName){
        StringBuffer str = new StringBuffer("<select name='" + selectName
                + "' id='" + selectID + "'");
        if(!"".equals(jsString)){
            str.append(" " + jsString + " >");
        } else{
            str.append(">");
        }
        if(isFull){
        	if(isFullName!=null&&isFullName.length>0){
        		 str.append("<option value='" + fullOptionsValue + "' "
                         + (util.isNull(selectedValue) ? "selected" : "")
                         + ">"+isFullName[0]+"</option>");
        	}else {
        		 str.append("<option value='" + fullOptionsValue + "' "
                         + (util.isNull(selectedValue) ? "selected" : "")
                         + ">不限</option>");
			}
        	
           
        }
        if(map!=null){
            Iterator<String> it = map.keySet().iterator();
            while(it.hasNext()){
                String key = (String) it.next();
                String value = (String) map.get(key);
                str.append("<option value='" + key + "' "
                        + (key.equals(selectedValue) ? "selected" : "") + ">"
                        + value + "</option>");
            }
        }

        
        
        str.append("</select>");
        return str.toString();
    }

    /**
     * radio生成器
     *
     * @param map
     * @param radioName
     * @param radioId
     * @param checkedValue
     * @param jsString
     *
     * @return
     */
    public static String radioGenerator(Map<String, String> map,
            String radioName, String radioId, String checkedValue,
            String jsString){
        StringBuffer str = new StringBuffer();
        Iterator<String> it = map.keySet().iterator();
        while(it.hasNext()){
            String key = (String) it.next();
            String value = (String) map.get(key);
            str.append("<input type='radio' style='width:15px;	border:0px;	float:left;'");
            if(!"".equals(jsString)){
                str.append(" onclick='" + jsString + "'");
            }
            str.append(" name='" + radioName + "' id='" + radioId + "' value='"
                    + key + "' " + (key.equals(checkedValue) ? "checked" : "")
                    + "><span style='float:left;'>" + value + "</span></input>");
        }
        return str.toString();
    }

    /**
     * 把String[] 转换为string型
     *
     * @param str
     */
    public static String strArrayToStr(String[] str){
        StringBuffer sb = new StringBuffer("");
        for(int i = 0; i < str.length; i++){
            sb.append(str[i].trim());
            if(i != str.length - 1){
                sb.append(",");
            }
        }
        return sb.toString().trim();
    }

    /**
     * 删除单个文件
     *
     * @param fileName文件名字
     */
    public static boolean delFile(String fileAdr){
        boolean b = true;
        File file = new File(fileAdr);
        if(file.exists() && file.isFile()){
            b = file.delete();
        }
        return b;
    }
    /**
     * 设置response格式
     *
     * @param response
     */
    public static void setResponse(HttpServletResponse response){
        response.setContentType("text/html;charset='utf-8'");
        response.setCharacterEncoding("utf-8");
        response.setHeader("charset", "utf-8");
    }
    
    /**
     * .net和java的xml序列化的相互转换
     *
     * @param xml
     * @param status
     *
     * @return
     */
    public static String setXml(String xml, String status,String BeanName){
        String xmlString = "";
        if(Const.ZERO.equals(status)){
            xmlString = xml.replace("<list>","<ArrayOf"+BeanName+" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">")
                    .replace("</list>", "</ArrayOf"+BeanName+">").replace("__","_");
            xmlString = "<?xml version=\"1.0\"?>\n" + xmlString;
        }
        if(Const.ONE.equals(status)){
            xmlString = xml.replace("ArrayOf"+BeanName+" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"","list").replace("ArrayOf"+BeanName, "list");
        }
        return xmlString;
    }
    
    //会议通知接口日期转换
    public static SimpleDateFormat parseSDF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat formatSDF=new SimpleDateFormat("yyyy/MM/dd");
    
    
}
